home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / idx-vector.h < prev    next >
C/C++ Source or Header  |  1997-08-01  |  6KB  |  261 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_idx_vector_h)
  24. #define octave_idx_vector_h 1
  25.  
  26. #if defined (__GNUG__)
  27. #pragma interface
  28. #endif
  29.  
  30. class ostream;
  31. class ColumnVector;
  32. class Matrix;
  33. class Range;
  34.  
  35. class
  36. idx_vector
  37. {
  38. private:
  39.  
  40.   class
  41.   idx_vector_rep
  42.   {
  43.   public:
  44.  
  45.     idx_vector_rep (void)
  46.       {
  47.     colon = 0;
  48.     len = 0;
  49.     num_zeros = 0;
  50.     num_ones = 0;
  51.     one_zero = 0;
  52.     orig_nr = 0;
  53.     orig_nc = 0;
  54.     initialized = 0;
  55.     frozen = 0;
  56.     colon_equiv_checked = 0;
  57.     colon_equiv = 0;
  58.     data = 0;
  59.       }
  60.  
  61.     idx_vector_rep (const ColumnVector& v);
  62.  
  63.     idx_vector_rep (const Matrix& m);
  64.  
  65.     idx_vector_rep (const Range& r);
  66.  
  67.     idx_vector_rep (double d);
  68.  
  69.     idx_vector_rep (char c);
  70.  
  71.     idx_vector_rep (const idx_vector_rep& a);
  72.  
  73.     ~idx_vector_rep (void) { delete [] data; }
  74.  
  75.     idx_vector_rep& operator = (const idx_vector_rep& a);
  76.  
  77.     int ok (void) { return initialized; }
  78.  
  79.     int capacity (void) const { return len; }
  80.     int length (int colon_len) const { return colon ? colon_len : len; }
  81.  
  82.     int elem (int n) const { return colon ? n : data[n]; }
  83.  
  84.     int checkelem (int n) const;
  85.     int operator () (int n) const { return checkelem (n); }
  86.  
  87.     int max (void) const { return max_val; }
  88.     int min (void) const { return min_val; }
  89.  
  90.     int one_zero_only (void) const { return one_zero; }
  91.     int zeros_count (void) const { return num_zeros; }
  92.     int ones_count (void) const { return num_ones; }
  93.  
  94.     int is_colon (void) const { return colon; }
  95.     int is_colon_equiv (int n, int sort_uniq);
  96.  
  97.     void sort (bool uniq);
  98.  
  99.     int orig_rows (void) const { return orig_nr; }
  100.     int orig_columns (void) const { return orig_nc; }
  101.  
  102.     // other stuff
  103.  
  104.     void shorten (int n); // Unsafe.  Avoid at all cost.
  105.  
  106.     int freeze (int z_len, const char *tag, int prefer_zero_one,
  107.         int resize_ok);
  108.  
  109.     // i/o
  110.  
  111.     ostream& print (ostream& os) const;
  112.  
  113.     int *data;
  114.     int len;
  115.     int num_zeros;
  116.     int num_ones;
  117.     int max_val;
  118.     int min_val;
  119.     int orig_nr;
  120.     int orig_nc;
  121.     int count;
  122.     int frozen_at_z_len;
  123.     int frozen_len;
  124.     unsigned int colon : 1;
  125.     unsigned int one_zero : 1;
  126.     unsigned int initialized : 1;
  127.     unsigned int frozen : 1;
  128.     unsigned int colon_equiv_checked : 1;
  129.     unsigned int colon_equiv : 1;
  130.  
  131.     void init_state (void);
  132.  
  133.     void maybe_convert_one_zero_to_idx (int z_len, int prefer_zero_one);
  134.   };
  135.  
  136. public:
  137.  
  138.   idx_vector (void)
  139.     {
  140.       rep = new idx_vector_rep ();
  141.       rep->count = 1;
  142.     }
  143.  
  144.   idx_vector (const ColumnVector& v)
  145.     {
  146.       rep = new idx_vector_rep (v);
  147.       rep->count = 1;
  148.     }
  149.  
  150.   idx_vector (const Matrix& m)
  151.     {
  152.       rep = new idx_vector_rep (m);
  153.       rep->count = 1;
  154.     }
  155.  
  156.   idx_vector (const Range& r)
  157.     {
  158.       rep = new idx_vector_rep (r);
  159.       rep->count = 1;
  160.     }
  161.  
  162.   idx_vector (double d)
  163.     {
  164.       rep = new idx_vector_rep (d);
  165.       rep->count = 1;
  166.     }
  167.  
  168.   idx_vector (char c)
  169.     {
  170.       rep = new idx_vector_rep (c);
  171.       rep->count = 1;
  172.     }
  173.  
  174.   idx_vector (const idx_vector& a)
  175.     {
  176.       rep = a.rep;
  177.       rep->count++;
  178.     }
  179.  
  180.   ~idx_vector (void)
  181.     {
  182.       if (--rep->count <= 0)
  183.     delete rep;
  184.     }
  185.  
  186.   idx_vector& operator = (const idx_vector& a)
  187.     {
  188.       if (this != &a)
  189.     {
  190.       if (--rep->count <= 0)
  191.         delete rep;
  192.  
  193.       rep = a.rep;
  194.       rep->count++;
  195.     }
  196.       return *this;
  197.     }
  198.  
  199.   operator void * () const { return (void *) rep->ok (); }
  200.  
  201.   int capacity (void) const { return rep->capacity (); }
  202.   int length (int cl) const { return rep->length (cl); }
  203.  
  204.   int elem (int n) const { return rep->elem (n); }
  205.   int checkelem (int n) const { return rep->checkelem (n); }
  206.   int operator () (int n) const { return rep->operator () (n); }
  207.  
  208.   int max (void) const { return rep->max (); }
  209.   int min (void) const { return rep->min (); }
  210.  
  211.   int one_zero_only (void) const { return rep->one_zero_only (); }
  212.   int zeros_count (void) const { return rep->zeros_count (); }
  213.   int ones_count (void) const { return rep->ones_count (); }
  214.  
  215.   int is_colon (void) const { return rep->is_colon (); }
  216.   int is_colon_equiv (int n, int sort_uniq = 0) const
  217.     { return rep->is_colon_equiv (n, sort_uniq); }
  218.  
  219.   void sort (bool uniq = false) { rep->sort (uniq); }
  220.  
  221.   int orig_rows (void) const { return rep->orig_rows (); }
  222.   int orig_columns (void) const { return rep->orig_columns (); }
  223.  
  224.   int orig_empty (void) const
  225.     {
  226.       return (! is_colon ()
  227.           && (orig_rows () == 0 || orig_columns () == 0));
  228.     }
  229.  
  230. // Unsafe.  Avoid at all cost.
  231.   void shorten (int n) { rep->shorten (n); }
  232.  
  233. // i/o
  234.  
  235.   int freeze (int z_len, const char *tag, int prefer_zero_one = 0,
  236.           int resize_ok = 0)
  237.     { return rep->freeze (z_len, tag, prefer_zero_one, resize_ok); }
  238.  
  239.   ostream& print (ostream& os) const { return rep->print (os); }
  240.  
  241.   friend ostream& operator << (ostream& os, const idx_vector& a)
  242.     { return a.print (os); }
  243.  
  244.   void maybe_convert_one_zero_to_idx (int z_len, int prefer_zero_one = 0)
  245.     { rep->maybe_convert_one_zero_to_idx (z_len, prefer_zero_one); }
  246.  
  247. private:
  248.  
  249.   idx_vector_rep *rep;
  250.  
  251.   void init_state (void) { rep->init_state (); }
  252. };
  253.  
  254. #endif
  255.  
  256. /*
  257. ;;; Local Variables: ***
  258. ;;; mode: C++ ***
  259. ;;; End: ***
  260. */
  261.